

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="若发现文章有误或有任何建议，敬请指教，感谢@[TOC]  一、DockerFIle基础知识 DockerFIle 是用来构建Docker镜像的文本文件，是一条条构建镜像所需的指令和参数构成的脚本。 构建三部曲： 1）编写DockerFile文件2）docker build命令构建镜像3）docker run 依照镜像运行容器实例 1.1 DockerFile编写规范DockerFile官方参照文">
<meta property="og:type" content="article">
<meta property="og:title" content="Docker 笔记3 Dockerfile文件常用命令 _ Docker的网络机制五大网络模式，bridge模式、host模式、none模式、container模式、自定义模式的概念与区别">
<meta property="og:url" content="http://example.com/2022/04/08/Docker%20%E7%AC%94%E8%AE%B03%20Dockerfile%E6%96%87%E4%BB%B6%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4%20_%20Docker%E7%9A%84%E7%BD%91%E7%BB%9C%E6%9C%BA%E5%88%B6%E4%BA%94%E5%A4%A7%E7%BD%91%E7%BB%9C%E6%A8%A1%E5%BC%8F%EF%BC%8Cbridge%E6%A8%A1%E5%BC%8F%E3%80%81host%E6%A8%A1%E5%BC%8F%E3%80%81none%E6%A8%A1%E5%BC%8F%E3%80%81container%E6%A8%A1%E5%BC%8F%E3%80%81%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A8%A1%E5%BC%8F%E7%9A%84%E6%A6%82%E5%BF%B5%E4%B8%8E%E5%8C%BA%E5%88%AB/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="若发现文章有误或有任何建议，敬请指教，感谢@[TOC]  一、DockerFIle基础知识 DockerFIle 是用来构建Docker镜像的文本文件，是一条条构建镜像所需的指令和参数构成的脚本。 构建三部曲： 1）编写DockerFile文件2）docker build命令构建镜像3）docker run 依照镜像运行容器实例 1.1 DockerFile编写规范DockerFile官方参照文">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ca540dd2d8b440fd99ba156d99e8fc64.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/154cff8174b64842ba847533f7f5b289.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/1d65aeb6d8b14fa4b6b7662ed7409a7c.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/67b754b1280a4d54a829ccc3b45f6fbf.png">
<meta property="article:published_time" content="2022-04-08T15:00:49.000Z">
<meta property="article:modified_time" content="2022-08-22T15:43:06.377Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Docker">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/ca540dd2d8b440fd99ba156d99e8fc64.png#pic_center">
  
  
  
  <title>Docker 笔记3 Dockerfile文件常用命令 _ Docker的网络机制五大网络模式，bridge模式、host模式、none模式、container模式、自定义模式的概念与区别 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Docker 笔记3 Dockerfile文件常用命令 _ Docker的网络机制五大网络模式，bridge模式、host模式、none模式、container模式、自定义模式的概念与区别"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-04-08 23:00" pubdate>
          2022年4月8日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          12k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          97 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Docker 笔记3 Dockerfile文件常用命令 _ Docker的网络机制五大网络模式，bridge模式、host模式、none模式、container模式、自定义模式的概念与区别</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <blockquote>
<p>若发现文章有误或有任何建议，敬请指教，感谢<br>@[TOC]</p>
</blockquote>
<h1 id="一、DockerFIle基础知识"><a href="#一、DockerFIle基础知识" class="headerlink" title="一、DockerFIle基础知识"></a>一、DockerFIle基础知识</h1><hr>
<p><code>DockerFIle</code> 是用来构建Docker镜像的文本文件，是一条条构建镜像所需的指令和参数构成的脚本。</p>
<p>构建三部曲：</p>
<p>1）编写DockerFile文件<br>2）docker build命令构建镜像<br>3）docker run 依照镜像运行容器实例</p>
<h2 id="1-1-DockerFile编写规范"><a href="#1-1-DockerFile编写规范" class="headerlink" title="1.1 DockerFile编写规范"></a>1.1 DockerFile编写规范</h2><p><a target="_blank" rel="noopener" href="https://docs.docker.com/engine/reference/builder/">DockerFile官方参照文档</a><br>1）每条保留字指令都<strong>必须为大写字母</strong>且后面要跟随至少一个参数<br>2）指令按照从上到下，顺序执行<br>3）#表示注释<br>4）每条指令都会创建一个新的镜像层并对镜像进行提交</p>
<h2 id="1-2-Docker执行DockerFile的大致流程"><a href="#1-2-Docker执行DockerFile的大致流程" class="headerlink" title="1.2 Docker执行DockerFile的大致流程"></a>1.2 Docker执行DockerFile的大致流程</h2><p>1）Docker从基础镜像运行一个容器<br>2）执行一条命令并对容器做出修改<br>3）执行类似docker commit的操作提交一个新的镜像层<br>4）docker在基于刚提交的镜像运行一个新容器<br>5）执行dockerfile的下一条指令直到所有指令都执行完成</p>
<h2 id="1-3-小总结"><a href="#1-3-小总结" class="headerlink" title="1.3 小总结"></a>1.3 小总结</h2><p>从应用软件的角度来看，Dockerfile、Docker镜像与Docker容器分别代表软件的三个不同阶段</p>
<ul>
<li>Dockerfile是软件的原材料</li>
<li>Docker镜像是软件的交付品</li>
<li>Docker容器则可以认为是软件镜像的运行状态即依照镜像运行的容器实例</li>
</ul>
<p>Dockerfile面向开发，Docker镜像称为交付标准，Docker容器则涉及部署于运维，三者缺一不可，合力充当Docker体系的基石，如下图所示。<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/1682ab721a81409c8c25ebbc5c22f032.png#pic_center">https://img-blog.csdnimg.cn/1682ab721a81409c8c25ebbc5c22f032.png#pic_center</a> &#x3D;60%x)</p>
<h1 id="二、DockerFile构建过程"><a href="#二、DockerFile构建过程" class="headerlink" title="二、DockerFile构建过程"></a>二、DockerFile构建过程</h1><hr>
<h2 id="2-1-DockerFile-常见命令"><a href="#2-1-DockerFile-常见命令" class="headerlink" title="2.1 DockerFile 常见命令"></a>2.1 DockerFile 常见命令</h2><table>
<thead>
<tr>
<th align="left">编号</th>
<th align="left">指令</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">1</td>
<td align="left">FROM</td>
<td align="left">基础镜像，表示当前新镜像是基于哪一个镜像的，指定一个已存在的镜像作为模板，dockerfile中第一条必须是FROM</td>
</tr>
<tr>
<td align="left">2</td>
<td align="left">MAINTAINER</td>
<td align="left">镜像维护者的姓名和邮箱地址</td>
</tr>
<tr>
<td align="left">3</td>
<td align="left">RUN</td>
<td align="left">容器构建时需要运行的命令两种格式：shell格式<code>$ RUN &lt;命令行命令&gt;</code>和exec格式<code>RUN [&quot;可执行文件&quot;, &quot;参数1&quot;, &quot;参数2&quot;]</code></td>
</tr>
<tr>
<td align="left">4</td>
<td align="left">EXPOSE</td>
<td align="left">当前容器对外暴露出的端口</td>
</tr>
<tr>
<td align="left">5</td>
<td align="left">OWRKDIR</td>
<td align="left">指定在创建容器后，进入容器后默认的工作目录，一个落脚点</td>
</tr>
<tr>
<td align="left">6</td>
<td align="left">USER</td>
<td align="left">指定该镜像哪一个用户去执行，若不指定则默认为root</td>
</tr>
<tr>
<td align="left">7</td>
<td align="left">ENV</td>
<td align="left">用于构建镜像过程中设置环境变量</td>
</tr>
<tr>
<td align="left">8</td>
<td align="left">VOLUME</td>
<td align="left">容器数据卷，用于数据保存和持久化工作</td>
</tr>
<tr>
<td align="left">9</td>
<td align="left">ADD</td>
<td align="left">将宿主机目录下的文件拷贝到镜像且会自动处理URL和解压tar压缩包</td>
</tr>
<tr>
<td align="left">10</td>
<td align="left">COPY</td>
<td align="left">类似ADD，拷贝文件和目录到镜像中，将从构建上下文目录中&lt;源路径&gt;的文件&#x2F;目录复制到新的一层镜像内的&lt;目标路径&gt;位置，同样有shell格式<code>COPY src dest</code>和exec格式 <code>COPY[&quot;src&quot;, &quot;dest&quot;]</code>，若dest路径不存在则会自动创建</td>
</tr>
<tr>
<td align="left">11</td>
<td align="left">CMD</td>
<td align="left">指定容器启动后要做的事情，同样有shell和exec两种格式，可以有多个CMD命令，参数会被docker run 之后的参数覆盖</td>
</tr>
<tr>
<td align="left">12</td>
<td align="left">ENTRYPOINT</td>
<td align="left">指定一个容器启动时要运行的命令类似于CMD指令，但是ENTRYPOINT不会被docker run 后面的命令覆盖，命令格式ENTRYPOINT <code>[&quot;&lt;executeable&gt;&quot;, &quot;&lt;param1&gt;&quot;,&quot;&lt;param2&quot;&gt;,...]</code></td>
</tr>
</tbody></table>
<p>当指定了ENTRYPOINT，CMD的含义就发生了变化，不再是直接运行其命令而是将CMD的内容作为参数传递给ENTRYPOINT指令，这两个组合则变成了 <code>&lt;ENTRYPOINT&gt; &lt;CMD&gt;</code></p>
<p>案例:(假设通过Dockerfile构建Nginx:test镜像)</p>
<figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs dockerfile"><span class="hljs-keyword">FROM</span> nginx<br><br><span class="hljs-keyword">ENTRYPOINT</span><span class="language-bash"> [<span class="hljs-string">&quot;nginx&quot;</span>, <span class="hljs-string">&quot;-c&quot;</span>]  <span class="hljs-comment"># 定参</span></span><br><span class="hljs-keyword">CMD</span><span class="language-bash"> [<span class="hljs-string">&quot;/etc/nginx/nginx.conf&quot;</span>] <span class="hljs-comment"># 变参</span></span><br></code></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th align="left">命令类型</th>
<th align="left">按照dockerfile编写执行</th>
<th align="left">传参运行</th>
</tr>
</thead>
<tbody><tr>
<td align="left">Docker命令</td>
<td align="left">docker run nginx:test</td>
<td align="left">docker run nginx:test -c &#x2F;etc&#x2F;nginx&#x2F;new.conf</td>
</tr>
<tr>
<td align="left">衍生出的实际命令</td>
<td align="left">nginx -c &#x2F;etc&#x2F;nginx&#x2F;nginx.conf</td>
<td align="left">nginx -c &#x2F;etc&#x2F;nginx&#x2F;new.conf</td>
</tr>
</tbody></table>
<p><strong>DockerFile命令类型的整理：</strong></p>
<table>
<thead>
<tr>
<th align="left">BUILD构建容器</th>
<th align="left">Both两者都存在</th>
<th align="left">RUN 启动容器</th>
</tr>
</thead>
<tbody><tr>
<td align="left">FROM</td>
<td align="left">WORKDIR</td>
<td align="left">CMD</td>
</tr>
<tr>
<td align="left">MAINTAINER</td>
<td align="left">USER</td>
<td align="left">ENV</td>
</tr>
<tr>
<td align="left">COPY</td>
<td align="left"></td>
<td align="left">EXPOSE</td>
</tr>
<tr>
<td align="left">ADD</td>
<td align="left"></td>
<td align="left">VOLUME</td>
</tr>
<tr>
<td align="left">RUN</td>
<td align="left"></td>
<td align="left">ENTRYPOINT</td>
</tr>
<tr>
<td align="left">ONBUILD</td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr>
<td align="left">.dockerignore</td>
<td align="left"></td>
<td align="left"></td>
</tr>
</tbody></table>
<h2 id="2-2-DockerFile使用案例"><a href="#2-2-DockerFile使用案例" class="headerlink" title="2.2 DockerFile使用案例"></a>2.2 DockerFile使用案例</h2><p>案例：使用Dockerfile构建一个具有vim、JDK环境的centos7镜像</p>
<p><strong>1）首先准备Docker系统镜像和所需的JDK压缩包</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker pull centos:7</span><br><span class="hljs-meta prompt_"></span><br><span class="hljs-meta prompt_">$ </span><span class="language-bash">wget https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/8/jdk/x64/linux/OpenJDK8U-jdk_x64_linux_hotspot_8u322b06.tar.gz</span><br></code></pre></td></tr></table></figure>
<p><strong>2）编写Dockerfile</strong></p>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs awk">FROM centos:<span class="hljs-number">7</span><br>MAINTAINER uni&lt;uni1024@qq.com&gt;<br> <br>ENV MYPATH <span class="hljs-regexp">/usr/</span>local<br>WORKDIR <span class="hljs-variable">$MYPATH</span><br> <br><span class="hljs-comment">#安装vim编辑器</span><br>RUN yum -y install vim<br><span class="hljs-comment">#安装ifconfig命令查看网络IP</span><br>RUN yum -y install net-tools<br><span class="hljs-comment">#安装java8及lib库</span><br>RUN yum -y install glibc.i686<br>RUN mkdir <span class="hljs-regexp">/usr/</span>local/java<br><span class="hljs-comment">#ADD 是相对路径jar,把OpenJDK8U-jdk_x64_linux_hotspot_8u322b06.tar.gz添加到容器中,安装包必须要和Dockerfile文件在同一位置</span><br>ADD .<span class="hljs-regexp">/OpenJDK8U-jdk_x64_linux_hotspot_8u322b06.tar.gz /u</span>sr<span class="hljs-regexp">/local/</span>java/<br><span class="hljs-comment">#配置java环境变量</span><br>ENV JAVA_HOME <span class="hljs-regexp">/usr/</span>local<span class="hljs-regexp">/java/</span>jdk8u322-b06<br>ENV JRE_HOME <span class="hljs-variable">$JAVA_HOME</span>/jre<br>ENV CLASSPATH <span class="hljs-variable">$JAVA_HOME</span><span class="hljs-regexp">/lib/</span>dt.jar:<span class="hljs-variable">$JAVA_HOME</span><span class="hljs-regexp">/lib/</span>tools.jar:<span class="hljs-variable">$JRE_HOME</span>/lib:<span class="hljs-variable">$CLASSPATH</span><br>ENV PATH <span class="hljs-variable">$JAVA_HOME</span>/bin:<span class="hljs-variable">$PATH</span><br> <br>EXPOSE <span class="hljs-number">80</span><br> <br>CMD echo <span class="hljs-variable">$MYPATH</span><br>CMD echo <span class="hljs-string">&quot;success--------------ok&quot;</span><br>CMD <span class="hljs-regexp">/bin/</span>bash<br></code></pre></td></tr></table></figure>

<p><strong>3）构建镜像</strong></p>
<blockquote>
<p>标准语法（注：TAG后的PATH表示DockerFile所在的目录)</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">&gt;</span><span class="language-bash">docker build -t 新镜像名称:TAG PATH</span><br></code></pre></td></tr></table></figure>
</blockquote>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker build -t centos7:v0.1 .</span><br></code></pre></td></tr></table></figure>
<p><strong>4）创建并运行容器</strong></p>
<blockquote>
<p>标准语法</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">&gt;</span><span class="language-bash">docker run -it 新镜像名称:TAG bash</span><br></code></pre></td></tr></table></figure>
</blockquote>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker run -it centos7:v0.1 bash</span><br></code></pre></td></tr></table></figure>
<p><strong>5) 测试效果</strong><br>测试vim、java的命令，测试结果:<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/5cd0c36a2002449b9cd4a3a5932fa4ed.png#">https://img-blog.csdnimg.cn/5cd0c36a2002449b9cd4a3a5932fa4ed.png#</a> &#x3D;60%x)<br>至此，DockerFile测试完毕，我们通过DockerFile构建了一个具有vim工具、JDK环境的centos7系统的镜像。</p>
<h1 id="三、虚悬镜像"><a href="#三、虚悬镜像" class="headerlink" title="三、虚悬镜像"></a>三、虚悬镜像</h1><hr>
<p>仓库名、标签都是<code>&lt;none&gt;</code>的镜像，俗称 <code>dangling image</code></p>
<p><strong>案例：</strong> 使用Dockerfile创建虚悬镜像<br><strong>1）编写 <code>Dockerfile</code></strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs shell">FROM ubuntu<br>CMD echo &#x27;action is success&#x27;<br></code></pre></td></tr></table></figure>
<p><strong>2）构建镜像</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker build .</span><br></code></pre></td></tr></table></figure>
<p><strong>3）查看当前所有的虚悬镜像</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker image <span class="hljs-built_in">ls</span> -f dangling=<span class="hljs-literal">true</span></span><br></code></pre></td></tr></table></figure>
<p>查看结果：<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/1851918a8463445da2d4845de0e943c4.png#">https://img-blog.csdnimg.cn/1851918a8463445da2d4845de0e943c4.png#</a> &#x3D;70%x)<br><strong>4）删除所有的虚悬镜像</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker image prune</span><br></code></pre></td></tr></table></figure>
<p>运行结果：<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/aebc0965581f445ca622e7c97cdca1b5.png#">https://img-blog.csdnimg.cn/aebc0965581f445ca622e7c97cdca1b5.png#</a> &#x3D;60%x)<br>至此，虚悬镜像的添加、删除测试完毕。</p>
<h1 id="四、Docker网络"><a href="#四、Docker网络" class="headerlink" title="四、Docker网络"></a>四、Docker网络</h1><hr>
<p>Docker服务默认会创建一个docker0网桥，（其上有一个docker0内部接口），该桥接网络的名称为docker0，它在内核层连通了其他的物理或虚拟网卡，这就将所有容器和本地主机都放到同一个物理网络。</p>
<p>Docker默认指定了docker0接口的IP地址和子网掩码，让主机和容器之间可通过网桥相互通信。</p>
<h2 id="4-1-Docker-network常用命令"><a href="#4-1-Docker-network常用命令" class="headerlink" title="4.1 Docker network常用命令"></a>4.1 Docker network常用命令</h2><p>docker服务启动后出现了名为docker0的虚拟网桥</p>
<p><strong>1）查看docker默认的网络模式（三个）</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker network <span class="hljs-built_in">ls</span></span> <br></code></pre></td></tr></table></figure>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/b1804e002bad413cb0f7d7d458ae9af9.png#">https://img-blog.csdnimg.cn/b1804e002bad413cb0f7d7d458ae9af9.png#</a> &#x3D;60%x)<br><strong>2）查看docker network相关命令</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker network --<span class="hljs-built_in">help</span></span><br></code></pre></td></tr></table></figure>
<p>运行结果:</p>
<blockquote>
<p>Commands:<br>  connect     Connect a container to a network<br>  create      Create a network<br>  disconnect  Disconnect a container from a network<br>  inspect     Display detailed information on one or more networks<br>  ls          List networks<br>  prune       Remove all unused networks<br>  rm          Remove one or more networks</p>
</blockquote>
<p>翻译后：</p>
<table>
<thead>
<tr>
<th align="left">命令</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">connect</td>
<td align="left">连接一个容器的网络</td>
</tr>
<tr>
<td align="left">create</td>
<td align="left">创建网络，默认为<code>bridge</code>网桥模式</td>
</tr>
<tr>
<td align="left">disconnect</td>
<td align="left">取消连接一个容器的网络</td>
</tr>
<tr>
<td align="left">inspect</td>
<td align="left">显示一个或多个网络上的详细信息</td>
</tr>
<tr>
<td align="left">ls</td>
<td align="left">列出所有的网络</td>
</tr>
<tr>
<td align="left">prune</td>
<td align="left">移除所有无效的网络（虚悬网络）</td>
</tr>
<tr>
<td align="left">rm</td>
<td align="left">删除一个或者多个网络</td>
</tr>
</tbody></table>
<p><strong>3）docker network常用命令：</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_"># </span><span class="language-bash">查看网络</span><br><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker network <span class="hljs-built_in">ls</span></span><br><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker network inpsect xxx网络名字(比如bridge)</span><br><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker network <span class="hljs-built_in">rm</span> xxx网络名字</span><br></code></pre></td></tr></table></figure>
<h2 id="4-2-Docker常见网络模式"><a href="#4-2-Docker常见网络模式" class="headerlink" title="4.2 Docker常见网络模式"></a>4.2 Docker常见网络模式</h2><p>常见网络模式</p>
<table>
<thead>
<tr>
<th align="left">网络模式</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">bridge</td>
<td align="left">为每一个容器分配、设置IP等，并将容器连接到一个docker0</td>
</tr>
<tr>
<td align="left">host</td>
<td align="left">容器将不会虚拟出自己的网卡，配置自己的IP等，而是使用宿主机的IP和端口</td>
</tr>
<tr>
<td align="left">none</td>
<td align="left">容器有独立的Network namespace，但并没有对其进行任何网络设置，如分配 veth</td>
</tr>
<tr>
<td align="left">container</td>
<td align="left">新创建的容器不会创建自己的网卡和配置自己的IP，而是和一个指定的容器共享IP、端口范围等</td>
</tr>
</tbody></table>
<p>注：在使用container时，语法为<code>--network container:NAME或容器ID</code></p>
<p><strong>案例：</strong> 查看某个容器的网络信息</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_"># </span><span class="language-bash">查看所有的容器</span><br><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker ps -a</span><br><span class="hljs-meta prompt_"></span><br><span class="hljs-meta prompt_"># </span><span class="language-bash">随便查看一个容器的网络配置（以redis-node-1为例)</span><br><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker inspect redis-node-1</span><br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight lasso"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs lasso"><span class="hljs-params">...</span> 前面内容过多 直接省略<br><span class="hljs-string">&quot;Networks&quot;</span>: &#123;<br>                <span class="hljs-string">&quot;host&quot;</span>: &#123;<br>                    <span class="hljs-string">&quot;IPAMConfig&quot;</span>: <span class="hljs-built_in">null</span>,<br>                    <span class="hljs-string">&quot;Links&quot;</span>: <span class="hljs-built_in">null</span>,<br>                    <span class="hljs-string">&quot;Aliases&quot;</span>: <span class="hljs-built_in">null</span>,<br>                    <span class="hljs-string">&quot;NetworkID&quot;</span>: <span class="hljs-string">&quot;3841c18eafca5926201531fcb793dbb85e568ed862b3f08b2460c010361c393a&quot;</span>,<br>                    <span class="hljs-string">&quot;EndpointID&quot;</span>: <span class="hljs-string">&quot;&quot;</span>,<br>                    <span class="hljs-string">&quot;Gateway&quot;</span>: <span class="hljs-string">&quot;&quot;</span>,<br>                    <span class="hljs-string">&quot;IPAddress&quot;</span>: <span class="hljs-string">&quot;&quot;</span>,<br>                    <span class="hljs-string">&quot;IPPrefixLen&quot;</span>: <span class="hljs-number">0</span>,<br>                    <span class="hljs-string">&quot;IPv6Gateway&quot;</span>: <span class="hljs-string">&quot;&quot;</span>,<br>                    <span class="hljs-string">&quot;GlobalIPv6Address&quot;</span>: <span class="hljs-string">&quot;&quot;</span>,<br>                    <span class="hljs-string">&quot;GlobalIPv6PrefixLen&quot;</span>: <span class="hljs-number">0</span>,<br>                    <span class="hljs-string">&quot;MacAddress&quot;</span>: <span class="hljs-string">&quot;&quot;</span>,<br>                    <span class="hljs-string">&quot;DriverOpts&quot;</span>: <span class="hljs-built_in">null</span><br>                &#125;<br>            &#125;<br>        &#125;<br>    &#125;<br><span class="hljs-meta">]</span><br></code></pre></td></tr></table></figure>

<p>可以看到这里的结果为JSON格式，其中NetWork键里的内容主要是配置网络相关的，所以通常就查看最后20行的内容，可使用<code>tail -n 行数</code>截取结果的最后二十行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker inspect redis-node-1 | <span class="hljs-built_in">tail</span> -n 20</span><br></code></pre></td></tr></table></figure>
<p><strong>查询结果分析：</strong></p>
<p>这里的查询结果显示为当前的<code>redis-node-1</code>容器的网络为<code>host</code>模式，说明是和主机共享网络，且发现<code>IPAddress</code>中对应的值为空字符串。</p>
<h2 id="4-3-Docker的bridge网络模式"><a href="#4-3-Docker的bridge网络模式" class="headerlink" title="4.3 Docker的bridge网络模式"></a>4.3 Docker的bridge网络模式</h2><blockquote>
<p>Docker使用Linux bridge桥接，在宿主机虚拟一个Docker容器网桥(docker0)<br>Docker启动一个容器时会根据Docker网桥的网段分配给容器一个IP地址，称为Container-IP，同时Docker网桥是每个容器的默认网关。因为在同一宿主机内的容器都接入同一个网桥，这样容器之间就能够通过容器的Container-IP直接通信。</p>
</blockquote>
<blockquote>
<p>docker run 命令未指定–network的情况下默认就是bridge模式，使用的就是docker0。<br>在宿主机通过<code>ifconfig</code>命令查看IP信息,就可以看到docker0和自己创建的自定义network比如eth0，eth1，eth2……它们分别代表网卡一，网卡二，网卡三……，而lo则代表127.0.0.1，即localhost，inet addr用来表示网卡的IP地址</p>
</blockquote>
<blockquote>
<p>网桥docker0创建1:1的虚拟设备接口一个叫veth，另一个叫eth0，成对匹配。</p>
<ul>
<li>整个宿主机的网桥模式都是docker0，类似一个交换机有一堆接口，每个接口叫veth，在本地主机和容器内分别创建一个虚拟接口，并让他们彼此联通（这样一对接口叫veth pair)</li>
<li>每个容器实例内部也有一块网卡，每个接口叫eth0</li>
<li>docker0上面的每个veth匹配某个容器实例内部的eth0</li>
</ul>
<p>综上，Docker将宿主机上的所有容器都连接到这个内部网络docker0上，两个容器在同一个网络下,会从这个网关里各自拿到分配的ip，此时两个容器的网络就是互通的。</p>
</blockquote>
<p> <strong>bridge模式下docker容器和宿主机网络互通示意图：</strong><br><img src="https://img-blog.csdnimg.cn/ca540dd2d8b440fd99ba156d99e8fc64.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><strong>案例：</strong> 创建两个Tomcat8的容器，查看IP信息<br><strong>1）创建两个Tomcat的容器</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker run -d -p 8081:8080 --name tomcat1 billygoo/tomcat8-jdk8</span><br><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker run -d -p 8082:8080 --name tomcat2 billygoo/tomcat8-jdk8</span><br></code></pre></td></tr></table></figure>
<p><strong>2）在两个终端进入两个容器</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_"># </span><span class="language-bash">终端1</span><br><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker <span class="hljs-built_in">exec</span> -it tomcat1 bash</span><br><span class="hljs-meta prompt_"># </span><span class="language-bash">终端2</span><br><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker <span class="hljs-built_in">exec</span> -it tomcat2 bash</span><br><span class="hljs-meta prompt_"># </span><span class="language-bash">终端1、2、3 查看IP信息</span><br>ip addr<br></code></pre></td></tr></table></figure>
<p><strong>查看结果：</strong><br><img src="https://img-blog.csdnimg.cn/154cff8174b64842ba847533f7f5b289.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>通过查看可以发现Docker容器的eth0是和docker0的veth一一对应的</p>
<h2 id="4-4-Docker的host主机模式"><a href="#4-4-Docker的host主机模式" class="headerlink" title="4.4 Docker的host主机模式"></a>4.4 Docker的host主机模式</h2><p>host主机模式，直接使用宿主机的IP地址与外界进行通信，不再需要额外进行NAT转换。容器将不会获得一个独立的 Network Namespace，而是和宿主机共用一个Network Namespace。同时，容器将不会虚拟出自己的网卡而是使用宿主机的IP和端口。<br><strong>host模式下docker容器和宿主机网络互通示意图：</strong><br><img src="https://img-blog.csdnimg.cn/1d65aeb6d8b14fa4b6b7662ed7409a7c.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><strong>案例：</strong> 创建host模式的tomcat容器</p>
<p><strong>1）使用创建host模式的容器</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker run -d -p 8083:8080 --network host --name tomcat3 billygoo/tomcat8-jdk8</span><br></code></pre></td></tr></table></figure>
<p>在创建后会出现警告</p>
<figure class="highlight asciidoc"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs asciidoc"><span class="hljs-symbol">WARNING: </span>Published ports are discarded when using host network mode<br></code></pre></td></tr></table></figure>
<p><strong>2）查看此时正在运行的容器</strong></p>
<p>此时查看运行的容器，发现刚刚创建的tomcat3在PORTS一栏为空，说明-p 8083:8080的端口映射配置失败。<br><img src="https://img-blog.csdnimg.cn/67b754b1280a4d54a829ccc3b45f6fbf.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><strong>结果分析：</strong> docker启动时若已经指定–network&#x3D;host或-net&#x3D;host，此时再指定-p映射端口就会出现之前的警告，因为在host模式下，-p设置的参数没有任何作用，端口号会以主机端口号为主，重复时则递增。</p>
<p><strong>解决方式：</strong> 切换docker容器的模式，例如指定–network&#x3D;bridge</p>
<p><strong>3）进入tomcat3容器查看ip信息</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker <span class="hljs-built_in">exec</span> -it tomcat3 bash</span><br><br>ip addr<br></code></pre></td></tr></table></figure>

<p>查询发现，host模式的容器，它的网络信息和Docker宿主机的网络信息类似</p>
<p><strong>4）接下来在查询tomcat3容器的网络信息</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">sudo docker inspect tomcat3 | tail -n 20<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs json">            <span class="hljs-attr">&quot;Networks&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>                <span class="hljs-attr">&quot;host&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>                    <span class="hljs-attr">&quot;IPAMConfig&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">null</span></span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;Links&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">null</span></span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;Aliases&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">null</span></span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;NetworkID&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;3841c18eafca5926201531fcb793dbb85e568ed862b3f08b2460c010361c393a&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;EndpointID&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;59b2b8dc37aaa402603d4cd6efa03293aefe8ac23eca03cefa224fbbf7432fa3&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;Gateway&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;IPAddress&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;IPPrefixLen&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;IPv6Gateway&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;GlobalIPv6Address&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;GlobalIPv6PrefixLen&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;MacAddress&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;DriverOpts&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">null</span></span><br>                <span class="hljs-punctuation">&#125;</span><br>            <span class="hljs-punctuation">&#125;</span><br>        <span class="hljs-punctuation">&#125;</span><br>    <span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">]</span><br></code></pre></td></tr></table></figure>
<p><strong>结果分析：</strong></p>
<p>可以看到 IPAddress 一栏为空</p>
<p>此时Tomcat3容器没有了端口映射，那么如何启动Tomcat服务器？</p>
<p>答案是：通过宿主机IP + 8080端口直接访问，如 <a target="_blank" rel="noopener" href="http://192.168.25.128:8080/">http://192.168.25.128:8080</a></p>
<p>由于不存在端口映射，那么端口就默认为8080了，这说明host模式不常用，容易出现端口冲突。</p>
<h2 id="4-5-Docker的none模式"><a href="#4-5-Docker的none模式" class="headerlink" title="4.5 Docker的none模式"></a>4.5 Docker的none模式</h2><p>none模式表示禁用网络功能，只有lo表示，即127.0.0.1表示本地回环。</p>
<p>在none模式下，并不为Docker容器进行任何网络配置，即这个容器没有网卡、IP、路由等信息，只有一个lo，需要我们自己为Docker容器添加网卡、配置IP等。</p>
<p><strong>案例：</strong> 创建none网络模式的Docker容器<br><strong>1）创建运行none模式的Docker容器</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker run -d -p 8084:8080 --network none --name tomcat4 billygoo/tomcat8-jdk8</span><br></code></pre></td></tr></table></figure>
<p><strong>2）查看none模式容器的网络信息：</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker inspect tomcat4 | <span class="hljs-built_in">tail</span> -n 20</span><br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs json">           <span class="hljs-attr">&quot;Networks&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>                <span class="hljs-attr">&quot;none&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>                    <span class="hljs-attr">&quot;IPAMConfig&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">null</span></span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;Links&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">null</span></span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;Aliases&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">null</span></span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;NetworkID&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;2221c1ce66e0d0e3d7a785c01b1f4f6334d92255738579aeff32c2e05d3ac197&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;EndpointID&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;d8490dd46cd3883be0118bf29dfab047d902c72ce701976c382c2b20fb7b21f5&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;Gateway&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;IPAddress&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;IPPrefixLen&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;IPv6Gateway&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;GlobalIPv6Address&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;GlobalIPv6PrefixLen&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;MacAddress&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;DriverOpts&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">null</span></span><br>                <span class="hljs-punctuation">&#125;</span><br>            <span class="hljs-punctuation">&#125;</span><br>        <span class="hljs-punctuation">&#125;</span><br>    <span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">]</span><br></code></pre></td></tr></table></figure>

<p><strong>3）进入容器查看网络信息</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker <span class="hljs-built_in">exec</span> -it tomcat4 bash</span><br><br>ip addr<br></code></pre></td></tr></table></figure>
<p><strong>运行结果（只有一个lo）：</strong></p>
<figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">1</span>: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu <span class="hljs-number">65536</span> qdisc noqueue state UNKNOWN group default qlen <span class="hljs-number">1000</span><br>    <span class="hljs-attribute">link</span>/loopback <span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span> brd <span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span><br>    <span class="hljs-attribute">inet</span> <span class="hljs-number">127.0.0.1</span>/<span class="hljs-number">8</span> scope host lo<br>       <span class="hljs-attribute">valid_lft</span> forever preferred_lft forever<br></code></pre></td></tr></table></figure>

<h2 id="4-6-Docker的container模式"><a href="#4-6-Docker的container模式" class="headerlink" title="4.6 Docker的container模式"></a>4.6 Docker的container模式</h2><blockquote>
<p>在container网络模式中，新创建的容器不会创建网卡和配置IP，而和一个指定的容器共享IP、端口范围等。</p>
</blockquote>
<blockquote>
<p>新建的容器和已存在的一个容器共享一个网络IP配置而不是和宿主机共享。</p>
</blockquote>
<blockquote>
<p>使用container模式的容器，它除了和指定容器在网络方面共享，其他的如文件系统、进程列表等依然是隔离的。</p>
</blockquote>
<p><strong>container模式下docker容器和宿主机网络互通示意图：</strong><br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/f20e4b8826ca4388ad06490725b5ac64.png#pic_center">https://img-blog.csdnimg.cn/f20e4b8826ca4388ad06490725b5ac64.png#pic_center</a> &#x3D;60%x)<br><strong>案例1：创建一个默认bridge模式的Tomcat容器和一个container模式的Tomcat容器</strong></p>
<p><strong>1）创建两个容器</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker run -d -p 8085:8080 --name tomcat5 billygoo/tomcat8-jdk8</span><br><span class="hljs-meta prompt_"></span><br><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker run -d -p 8086:8080 --network container:tomcat5 --name tomcat6 billygoo/tomcat8-jdk8</span><br></code></pre></td></tr></table></figure>
<p>创建失败，出现报错</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs routeros">docker: <span class="hljs-built_in">Error</span> response <span class="hljs-keyword">from</span> daemon: conflicting options:<span class="hljs-built_in"> port </span>publishing <span class="hljs-keyword">and</span> the container<span class="hljs-built_in"> type network </span>mode.<br></code></pre></td></tr></table></figure>
<p>注意，在上述情况中，相当于tomcat6和tomcat5公用了同一个IP和同一个端口（8080），导致端口冲突，本案例中使用tomcat演示不合适。</p>
<p><strong>案例2：使用Alpine操作系统</strong></p>
<p>Alpine操作系统是一个面向安全的轻型Linux发行版。</p>
<blockquote>
<p>Apline Linux是一款独立的、非商业的通用Linux发行版，专为追求安全性、简单性和资源效率的用户设计。大小不到6MB，特别适合容器打包</p>
</blockquote>
<p><strong>1）创建alpine容器</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_"># </span><span class="language-bash">终端1</span><br><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker run -it --name alpine1 alpine /bin/sh</span><br><span class="hljs-meta prompt_"># </span><span class="language-bash">终端2</span><br><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker run -it --network container:alpine1 --name alpine2 alpine /bin/sh</span><br></code></pre></td></tr></table></figure>

<p><strong>2）<code>ip addr</code>查看容器网络信息</strong></p>
<figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-comment"># alpine1 容器的网络信息</span><br><span class="hljs-attribute">33</span>: eth0@if34: &lt;BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN&gt; mtu <span class="hljs-number">1500</span> qdisc noqueue state UP <br>    <span class="hljs-attribute">link</span>/ether <span class="hljs-number">02</span>:<span class="hljs-number">42</span>:ac:<span class="hljs-number">11</span>:<span class="hljs-number">00</span>:<span class="hljs-number">02</span> brd ff:ff:ff:ff:ff:ff<br>    <span class="hljs-attribute">inet</span> <span class="hljs-number">172.17.0.2</span>/<span class="hljs-number">16</span> brd <span class="hljs-number">172.17.255.255</span> scope global eth0<br>       <span class="hljs-attribute">valid_lft</span> forever preferred_lft forever<br><br><span class="hljs-comment"># alpine2 容器的网络信息</span><br><span class="hljs-attribute">33</span>: eth0@if34: &lt;BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN&gt; mtu <span class="hljs-number">1500</span> qdisc noqueue state UP <br>    <span class="hljs-attribute">link</span>/ether <span class="hljs-number">02</span>:<span class="hljs-number">42</span>:ac:<span class="hljs-number">11</span>:<span class="hljs-number">00</span>:<span class="hljs-number">02</span> brd ff:ff:ff:ff:ff:ff<br>    <span class="hljs-attribute">inet</span> <span class="hljs-number">172.17.0.2</span>/<span class="hljs-number">16</span> brd <span class="hljs-number">172.17.255.255</span> scope global eth0<br>       <span class="hljs-attribute">valid_lft</span> forever preferred_lft forever<br></code></pre></td></tr></table></figure>

<p>此时如果关闭容器alpine1，那么alpine2里的依赖网段也就消失了，只剩下lo。</p>
<h2 id="4-7-Docker的自定义网络模式"><a href="#4-7-Docker的自定义网络模式" class="headerlink" title="4.7 Docker的自定义网络模式"></a>4.7 Docker的自定义网络模式</h2><p><strong>案例：创建自定义的docker网络（默认为bridge模式）</strong><br><strong>1）创建Docker自定义网络</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker network create uni_network</span><br></code></pre></td></tr></table></figure>
<p><strong>2）使用自定义的网络模式创建容器</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker run -d -p 8081:8080 --network uni_network --name tomcat1 billygoo/tomcat8-jdk8</span><br><span class="hljs-meta prompt_"></span><br><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker run -d -p 8082:8080 --network uni_network --name tomcat2 billygoo/tomcat8-jdk8</span><br></code></pre></td></tr></table></figure>
<p><strong>3）进入tomcat1容器，通过容器名ping通tomcat2的网络</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo docker <span class="hljs-built_in">exec</span> -it tomcat1 bash</span><br><br>ping tomcat2<br></code></pre></td></tr></table></figure>
<p>注：Docker默认的bridge模式无法支持通过服务名（即容器名）互通IP，只能通过IP，但是IP会随时变化</p>
<p><strong>结论：</strong> 自定义模式的网络本身维护好了主机名和IP的对应关系，使得Docker容器之间IP和域名都能ping通</p>
<h1 id="参-考-资-料"><a href="#参-考-资-料" class="headerlink" title="参  考  资  料"></a>参  考  资  料</h1><hr>
<p>[1] <a target="_blank" rel="noopener" href="https://docs.docker.com/get-started/">Docker官方使用文档</a><br>[2] <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1gr4y1U7CY?p=57">视频资料</a></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/Docker/" class="category-chain-item">Docker</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Docker/">#Docker</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Docker 笔记3 Dockerfile文件常用命令 _ Docker的网络机制五大网络模式，bridge模式、host模式、none模式、container模式、自定义模式的概念与区别</div>
      <div>http://example.com/2022/04/08/Docker 笔记3 Dockerfile文件常用命令 _ Docker的网络机制五大网络模式，bridge模式、host模式、none模式、container模式、自定义模式的概念与区别/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年4月8日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/04/09/Docker%20%E7%AC%94%E8%AE%B04%20Compose%20%E5%AE%B9%E5%99%A8%E7%BC%96%E6%8E%92/" title="Docker 笔记4 compose 容器编排">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Docker 笔记4 compose 容器编排</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/04/06/Hadoop%20HA%E9%AB%98%E5%8F%AF%E7%94%A8%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89%20HDFS-HA%20%E6%89%8B%E5%8A%A8%E3%80%81%E8%87%AA%E5%8A%A8%E6%A8%A1%E5%BC%8F%20_%20YARN-HA%20%E8%87%AA%E5%8A%A8%E6%A8%A1%E5%BC%8F%20_%20%E6%A8%A1%E6%8B%9F%E5%AE%95%E6%9C%BA%E6%95%85%E9%9A%9C%20_%20HA-%E5%9F%BA%E4%BA%8EZookeeper/" title="Hadoop HA高可用 学习笔记（一） HDFS-HA 手动、自动模式 _ YARN-HA 自动模式 _ 模拟宕机故障 _ HA-基于Zookeeper">
                        <span class="hidden-mobile">Hadoop HA高可用 学习笔记（一） HDFS-HA 手动、自动模式 _ YARN-HA 自动模式 _ 模拟宕机故障 _ HA-基于Zookeeper</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
